package com.mb.note.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lijian.common.domain.R;
import com.lijian.common.util.BeanUtils;
import com.mb.note.domain.dto.NoteDto;
import com.mb.note.domain.po.Note;
import com.mb.note.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiJian
 * @date 2024-11-04
 */
@Api(tags = "文档相关接口")
@RestController
@RequestMapping("/note")
@RequiredArgsConstructor
public class NoteController {
    private final NoteService noteService;
    @ApiOperation("新增笔记")
    @PostMapping("add")
    public boolean saveNote(@RequestBody NoteDto note) {
        // 新增
       boolean save =noteService.save(BeanUtils.copyBean(note, Note.class));
        return save;
    }
    @ApiOperation("获取集合笔记列表")
    @GetMapping("getNoteList")
    public R<?> getNoteList(@RequestParam String fid) {
        return R.ok(noteService.getNoteTitleList(fid));
    }
    @ApiOperation("获取笔记page")
    @RequestMapping("/getNoteByPage")
    public  R<IPage<Note>> getUser(@RequestParam(name = "current", defaultValue="1",required = false) int current,
                                   @RequestParam(name = "size", defaultValue="2",required = false) int size,
                                   @RequestParam(name = "title", required = false) String name){
        R<IPage<Note>> result = new R<>();
        Page<Note> page = new Page<>(current, size);
        IPage<Note> pageList ;
        QueryWrapper<Note> queryWrapper = new QueryWrapper();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("title", name);
        }
        pageList = noteService.page(page, queryWrapper);
        return result.ok(pageList);
    }
}
